﻿<template>
    <el-dialog
        :title="titleMap[mode]"
        v-model="visible"
        :width="1400"
        destroy-on-close
        @closed="$emit('closed')"
    >
        <el-form
            :model="entityObj"
            :rules="rules"
            :disabled="mode == 'Show'"
            ref="dialogForm"
            label-width="100px"
        >
            <el-row>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="供应链物料" prop="materialItemId">
                        <sc-material-select
                            ref="materialSelect"
                            :selectKeyValue="'materialItemId'"
                            :multiple="false"
                            :queryType="'material/item-pages'"
                            :queryList="{}"
                            :props="materialProps"
                            v-model="entityObj.materials"
                            @fatherMethod="materialSelectProcess"
                        ></sc-material-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="POS点单商品" prop="itemCode">
                        <sc-goods-price-select
                            ref="goodsSelect"
                            :selectKeyValue="'itemCode'"
                            :multiple="false"
                            v-model="entityObj.goods"
                            @fatherMethod="goodsSelectProcess"
                        ></sc-goods-price-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
               <el-col :span="12">
                    <el-form-item label-width="120px" label="物料编码" prop="materialCode">
                        {{entityObj.materialCode}}
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="点单商品编码" prop="goodCode">
                        {{entityObj.goodCode}}
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
               <el-col :span="12">
                    <el-form-item label-width="120px" label="物料名称" prop="materialName">
                        {{entityObj.materialName}}
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="点单商品名称" prop="goodName">
                        {{entityObj.goodName}}
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
               <el-col :span="12">
                    <el-form-item label-width="120px" label="物料规格" prop="materialSpecs">
                        {{entityObj.materialSpecs}}
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="点单商品规格" prop="standard">
                        {{entityObj.standard}}
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
               <el-col :span="12">
                    <el-form-item label-width="120px" label="物料单位" prop="unitName" style="color: red">
                        {{entityObj.unitName}}
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="点单商品单位" prop="uomName" style="color: red">
                        {{entityObj.uomName}}
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
               <el-col :span="24" style="color: red">
                <el-form-item label-width="120px" label="" prop="uomName" style="color: red">
                    提醒：物料名称/品名/规格/单位要对应
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <template #footer>
            <el-button @click="visible = false">取 消</el-button>
            <el-button
                v-if="mode != 'Show'"
                type="primary"
                :loading="isSaveing"
                @click="submit()"
            >保 存</el-button>
        </template>
    </el-dialog>
</template>

<script>
import tool from "@/utils/tool";
export default {
    emits: ["success", "closed"],
    data() {
        return {
            mode: "Add",
            titleMap: {
                Add: "新增",
                Edit: "编辑",
                Show: "查看",
            },
            materialProps:{
                label: "materialName",
                value: "materialItemId",
                keyword: "keyword",
            },
            entityObj: {
                minStock: 0,
                minDeliveryQty: 1,
                isSpecial: false,
                status: 1,
                materialItems: [
                    { unitCode: "", unitName: "", isMinUnit: true, unitQty: 0 },
                ],
            },
            visible: false,
            isSaveing: false,
            needSaveFlag: false,
            //验证规则
            rules: {
                itemCode: [
                    {
                        required: true,
                        type: "string",
                        message: "请填写",
                        trigger: "change",
                    },
                ],
                materialItemId: [
                    {
                        required: true,
                        type: "number",
                        message: "请填写",
                        trigger: "change",
                    },
                ],
            },
            materialTypeTree: [],
            optionsBrand: [],
            optionsUnit: [],
            applyOrderQueryList: {},
        };
    },
    methods: {
        //显示=
        open(mode = "Add") {
            this.mode = mode;
            this.visible = true;
            return this;
        },
        goodsSelectProcess(rowData) {
            this.entityObj.goods = rowData;
            this.entityObj.itemCode = rowData.itemCode;
            this.entityObj.goodCode = rowData.goodsCode;
            this.entityObj.goodName = rowData.goodsName;
            this.entityObj.standard = rowData.standard;
            this.entityObj.uomCode = rowData.uomCode;
            this.entityObj.uomName = rowData.uomName;
        },
        materialSelectProcess(rowData) {
            this.entityObj.materials = rowData;
            this.entityObj.materialId = rowData.materialId;
            this.entityObj.materialItemId = rowData.materialItemId;
            this.entityObj.materialCode = rowData.materialCode;
            this.entityObj.materialName = rowData.materialName;
            this.entityObj.materialSpecs = rowData.materialSpecs;
            this.entityObj.unitCode = rowData.unitCode;
            this.entityObj.unitName = rowData.unitName;
        },
        //表单提交方法
        submit() {
            this.$refs.dialogForm.validate(async (valid) => {
                if (valid) {
                    try {
                        var returnValue =
                            await this.$API.auth.PostProcessApi.post(
                                this.mode == "Add"
                                    ? "pos-material-compare/create"
                                    : "pos-material-compare/update",
                                { ...this.entityObj }
                            );
                        if (returnValue.status == 200) {
                            this.$message.success("操作成功");
                            this.$emit("fatherMethod");
                            this.isSaveing = false;
                            this.visible = false;
                        } else {
                            this.$message.error("操作失败:" + returnValue.msg);
                            this.isSaveing = false;
                        }
                    } catch (error) {
                        this.loading = false;
                        return false;
                    }
                }
            });
        },
        //表单注入数据
        async setData(data) {
            this.entityObj = {
                ...data,
                goods:{
                    itemCode:data.itemCode,
                    goodsName:data.goodName
                },
                materials:{
                    materialItemId:data.materialItemId,
                    materialName:data.materialName
                }
            };
            console.log(this.entityObj); 
        },
        async initOptions() {
            
        },
    },
    async mounted() {
        this.entityObj.storeName = tool.data.get("USER_INFO").OrgName;
        this.entityObj.storeId = tool.data.get("USER_INFO").orgId;
        await this.initOptions();
    },
};
</script>

<style></style>
